﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Runtime.Serialization;
using System.ComponentModel;

namespace elr_tracking_bal.CAT
{
    public class Banco : INotifyPropertyChanged
    {
        private int _ID_BANCO;
        private string _BANCO;

        public int ID_BANCO
        {
            get
            {
                return _ID_BANCO;
            }
            set
            {
                if (_ID_BANCO != value)
                {
                    _ID_BANCO = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("ID_BANCO"));
                    }
                }

            }
        }

        public string BANCO
        {
            get
            {
                return _BANCO;
            }
            set
            {
                if (_BANCO != value)
                {
                    _BANCO = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("BANCO"));
                    }
                }

            }
        }

        /// <summary>
        /// Método que ejecuta el store para recuperar Bancos
        /// </summary>
        /// <returns></returns>
        public static DataTable GetData()
        {
            DataTable dt;
            dt = elr_tracking_dal.SrvDB.ExecuteQuery("exec SP_GETLIST_CAT_BANCO");
            return dt;
        }
        /// <summary>
        /// Método que obtiene la lista de Bancos
        /// </summary>
        /// <returns></returns>
        public static List<Banco> GetDataBanco()
        {
            try
            {
                DataTable dt;
                dt = Banco.GetData();
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    List<Banco> list = dt.AsEnumerable().Select(
                        row => new Banco
                        {
                            ID_BANCO = row.Field<int?>("ID_BANCO").GetValueOrDefault(),
                            BANCO = String.IsNullOrEmpty(row.Field<string>("BANCO")) ? "" : row.Field<string>("BANCO"),
                        }
                        ).ToList();
                    return list;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Método para insertar Banco
        /// </summary>
        /// <param name="BANCO"></param>
        /// <returns></returns>
        public static elr_tracking_bal.UpsertStateMsg InsertBanco(Banco cat)
        {
            Banco inser;
            UpsertStateMsg inserCat;
            try
            {
                int dt = elr_tracking_dal.SrvDB.ExecuteNonQuery("exec SP_INSERT_CAT_BANCO '" + cat.BANCO + "'");

                inser = new Banco(cat.BANCO);
                inserCat = new UpsertStateMsg("Registros guardados " + dt.ToString(), true);
                return inserCat;
            }
            catch (Exception ex)
            {
                inserCat = new UpsertStateMsg("Error al guardar " + ex, false);
                return inserCat;
            }
        }
        /// <summary>
        /// Método para borrar variod Bancos
        /// </summary>
        /// <param name="ID_BANCO"></param>
        /// <returns></returns>
        public static elr_tracking_bal.DeleteStateMsg DeleteBanco(string[] ID_BANCO)
        {
            elr_tracking_bal.DeleteStateMsg delete;
            string join;
            if (ID_BANCO == null)
            {
                join = "";
            }
            else
            {
                join = String.Join(",", ID_BANCO);
            }
            try
            {
                int dt = elr_tracking_dal.SrvDB.ExecuteNonQuery("exec SP_DELETE_CAT_BANCO '" + join + "'");
                delete = new elr_tracking_bal.DeleteStateMsg("Registro eliminado" + dt.ToString(), true);
                return delete;
            }
            catch (Exception ex)
            {
                delete = new elr_tracking_bal.DeleteStateMsg("Error al eliminar" + ex, false);
                return delete;
            }
        }
        /// <summary>
        /// Método para modificar Banco
        /// </summary>
        /// <param name="cat"></param>
        /// <returns></returns>
        public static elr_tracking_bal.UpsertStateMsg UpdateBanco(Banco cat)
        {
            Banco update;
            UpsertStateMsg updateCat;
            try
            {
                int dt = elr_tracking_dal.SrvDB.ExecuteNonQuery("exec SP_UPDATE_CAT_BANCO " + cat.ID_BANCO + ",'" + cat.BANCO + "'");

                update = new Banco(cat.ID_BANCO, cat.BANCO);
                updateCat = new UpsertStateMsg("Registros actualizados " + dt.ToString(), true);
                return updateCat;
            }
            catch (Exception ex)
            {
                updateCat = new UpsertStateMsg("Error al actualizar " + ex, false);
                return updateCat;
            }
        }
        /// <summary>
        /// Método para obtener un Banco    
        /// </summary>
        /// <param name="ID_BANCO"></param>
        /// <returns></returns>
        public static Banco GetOneBanco(int ID_BANCO)
        {
            try
            {
                Banco cat;
                DataTable dt = elr_tracking_dal.SrvDB.ExecuteQuery("exec SP_GETONE_CAT_BANCO " + ID_BANCO + "");
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else if (ID_BANCO == 0)
                {
                    return null;
                }
                else
                {
                    cat = new Banco(Convert.ToInt32(dt.Rows[0]["ID_BANCO"]), Convert.ToString(dt.Rows[0]["BANCO"]));
                }
                return cat;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Constructor sin parámetros
        /// </summary>
        public Banco() { }
        /// <summary>
        /// Constructor con un parámetro
        /// </summary>
        /// <param name="BANCO"></param>
        public Banco(string BANCO) {
            this._BANCO = BANCO;
        }
        /// <summary>
        /// Constructor con dos parámetros
        /// </summary>
        /// <param name="ID_BANCO"></param>
        /// <param name="BANCO"></param>
        public Banco(int ID_BANCO, string BANCO)
        {
            this._ID_BANCO = ID_BANCO;
            this._BANCO = BANCO; 
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}